package cn.iocoder.yudao.module.mqtt.service.commport;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.mqtt.controller.admin.commport.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.commport.CommPortDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.mqtt.dal.mysql.commport.CommPortMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.mqtt.enums.ErrorCodeConstants.*;

/**
 * 通讯口配置 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class CommPortServiceImpl implements CommPortService {

    @Resource
    private CommPortMapper commPortMapper;

    @Override
    public Long createCommPort(CommPortSaveReqVO createReqVO) {
        // 插入
        CommPortDO commPort = BeanUtils.toBean(createReqVO, CommPortDO.class);
        commPortMapper.insert(commPort);
        // 返回
        return commPort.getId();
    }

    @Override
    public void updateCommPort(CommPortSaveReqVO updateReqVO) {
        // 校验存在
        validateCommPortExists(updateReqVO.getId());
        // 更新
        CommPortDO updateObj = BeanUtils.toBean(updateReqVO, CommPortDO.class);
        commPortMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommPort(Long id) {
        // 校验存在
        validateCommPortExists(id);
        // 删除
        commPortMapper.deleteById(id);
    }

    @Override
        public void deleteCommPortListByIds(List<Long> ids) {
        // 校验存在
        validateCommPortExists(ids);
        // 删除
        commPortMapper.deleteByIds(ids);
        }

    private void validateCommPortExists(List<Long> ids) {
        List<CommPortDO> list = commPortMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COMM_PORT_NOT_EXISTS);
        }
    }

    private void validateCommPortExists(Long id) {
        if (commPortMapper.selectById(id) == null) {
            throw exception(COMM_PORT_NOT_EXISTS);
        }
    }

    @Override
    public CommPortDO getCommPort(Long id) {
        return commPortMapper.selectById(id);
    }

    @Override
    public PageResult<CommPortDO> getCommPortPage(CommPortPageReqVO pageReqVO) {
        return commPortMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CommPortDO> getSimple() {
        return commPortMapper.selectSimpleList();
    }

}